package com.laker.admin.module.excel.application.impl;

import com.laker.admin.module.excel.application.UserApplication;
import com.laker.admin.module.excel.domain.User;
import com.laker.admin.module.excel.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;


/**
 * @author : nihongxin
 * @date : 2021/3/22
 */
@Component
public class UserApplicationImpl implements UserApplication {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> getUserList() {
        return userRepository.findAll();
    }

    @Override
    public void saveUserEntity(User user) {
        userRepository.save(user);
    }

    @Override
    public List<User> getUserListWithNotDel() {
        Specification<User> specification = (root, query, criteriaBuilder) -> criteriaBuilder.and(
                criteriaBuilder.equal(root.get("delFlag"), false)
        );
        return userRepository.findAll(specification);
    }

    @Override
    public List<User> getUserListByUserIdListWithNotDel(Set<Long> userIds) {
        Specification<User> specification = (root, query, criteriaBuilder) -> criteriaBuilder.and(
                criteriaBuilder.equal(root.get("delFlag"), false),
                root.get("id").in(userIds)
        );
        return userRepository.findAll(specification);
    }

    @Override
    public List<User> getUserListBySystemId() {
        return userRepository.findAll();
    }

    @Override
    public User getUserByName(String userName) {
       return userRepository.getUserByName(userName);
    }


}
